﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;


namespace HardwareAs
{
    public class CustomerService
    {
        private List<Customer> Customers
        {
            get
            {
                List<Customer> customers;
                if (HttpContext.Current.Session["Customers"] != null)
                {
                    customers = (List<Customer>)HttpContext.Current.Session["Customers"];
                }
                else
                {
                    //Create customer data store and save in session
                    customers = new List<Customer>();

                    InitCustomerData(customers);

                    HttpContext.Current.Session["Customers"] = customers;
                }

                return customers;
            }
        }


        public Customer GetByID(int customerID)
        {
            return this.Customers.AsQueryable().First(customer => customer.ID == customerID);
        }


        public IList<Customer> GetAll()
        {
            return this.Customers;
        }


        public IQueryable<Customer> GetQueryable()
        {
            return this.Customers.AsQueryable();
        }


        public void Add(Customer customer)
        {
            customer.ID = this.Customers.Count + 1;
            this.Customers.Add(customer);
        }


        public void Update(Customer customer)
        {
            HardwareLinqDataContext db = new HardwareLinqDataContext();
            db.UpdateCustomer(customer.ID, customer.Email, customer.FirstName, customer.LastName, customer.address, customer.age, customer.Phone);

        }


        public void Delete(int customerID)
        {
            this.Customers.RemoveAll(customer => customer.ID == customerID);
        }


        private void InitCustomerData(List<Customer> customers)
	{
        HardwareLinqDataContext db = new HardwareLinqDataContext();
          
        List<HardwareAs.SelectAllNormalUsersResult> db2 =db.SelectAllNormalUsers().ToList();

        foreach (SelectAllNormalUsersResult temp in db.SelectAllNormalUsers())
        { 
            customers.Add(new Customer
            {
            ID=temp.ID,FirstName=temp.FirstName,LastName=temp.LastName,Phone=temp.Phone,Email=temp.Email,age=temp.age,DateOfBirth=temp.DateOfBirth
          
            });
        
        }
        return;
	
	}
    }
}
